package io.renren.modules.app.controller;

import java.util.*;

import io.lettuce.core.output.SocketAddressOutput;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.app.entity.BsDishEntity;
import io.renren.modules.app.entity.BsDishEntityVo;
import io.renren.modules.app.entity.DishFileVo;
import io.renren.modules.app.service.BsDishService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


/**
 * 菜品表
 *
 */
@RestController
@RequestMapping("tb_bs_dish/bsdish")
public class BsDishController {
    @Autowired
    private BsDishService bsDishService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("tb_bs_dish:bsdish:list")
    public R list(@RequestParam Map<String, Object> params, HttpServletRequest request){
        PageUtils page = bsDishService.queryPage(params,request);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("tb_bs_dish:bsdish:info")
    public R info(@PathVariable("id") Long id,HttpServletRequest request){
        BsDishEntity bsDish = bsDishService.queryById(id,request);
		//BsDishEntity bsDish = bsDishService.getById(id);

        return R.ok().put("bsDish", bsDish);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("tb_bs_dish:bsdish:save")
    public R save(@RequestBody BsDishEntity bsDish){
        bsDishService.saveDish(bsDish);
		//bsDishService.save(bsDish);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("tb_bs_dish:bsdish:update")
    public R update(@RequestBody BsDishEntity bsDish){
        bsDishService.updateDish(bsDish);
		//bsDishService.updateById(bsDish);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("tb_bs_dish:bsdish:delete")
    public R delete(@RequestBody Long[] ids){
		bsDishService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 图片上传接口
     */
    @RequestMapping(value = "dishUpload",method = RequestMethod.POST)
    public R dishUpload(MultipartFile file){
//        System.out.println(name1);
//        System.out.println(file);
//        System.out.println(map);
//        String originalFilename = file.getOriginalFilename();
//        System.out.println(originalFilename);
        String url = bsDishService.dishUpload(file);
        System.out.println(file);
        //
        return R.ok(url) ;
    }



}
